/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.BusBindingWrapper;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaBusBinding;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaNormalJms;
import han.bifang.common.entity.SoaServiceBus;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class BusBindingDao {

	public static void createBinding(SoaBusBinding sc, SoaHttpBinding hb)
			throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().save(hb);
			sc.setBindingId(hb.getId());
			DaoManager.getDao().save(sc);
			DaoManager.getDao().flush();
			tx.commit();
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusBindingDao.class);
			logger.error("BusBindingDao.createBinding(http)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createBinding(http) error", e);
		} finally {
			tx.end();
		}

	}

	public static void createBinding(SoaBusBinding sc, SoaJmsBindingWrapper hb)
			throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			SoaNormalJms info = (SoaNormalJms) hb.getInfo();

			DaoManager.getDao().save(info);

			hb.getJmsBinding().setInfoId(info.getId());

			DaoManager.getDao().save(hb.getJmsBinding());

			sc.setBindingId(hb.getJmsBinding().getId());
			DaoManager.getDao().save(sc);

			DaoManager.getDao().flush();
			tx.commit();
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusBindingDao.class);
			logger.error("BusBindingDao.createBinding(jms)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createBinding(jms) error", e);
		} finally {
			tx.end();
		}

	}

	public static void createBinding(SoaBusBinding sc, SoaSoapBinding hb)
			throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().save(hb);
			sc.setBindingId(hb.getId());

			DaoManager.getDao().save(sc);
			DaoManager.getDao().flush();
			tx.commit();
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusBindingDao.class);
			logger.error("BusBindingDao.createBinding(soap)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createBinding(soap) error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeBindingByBusId(String id) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String hql = "from SoaBusBinding s where s.busId = ?";
			List ret = DaoManager.getDao().find(hql, id);
			for (int i = 0, j = ret.size(); i < j; i++) {
				SoaBusBinding s = (SoaBusBinding) ret.get(i);
				if (s.getType().equals(EntityConstants.BINDING_HTTP)) {
					Object b = DaoManager.getDao().get(SoaHttpBinding.class,
							s.getBindingId());
					DaoManager.getDao().delete(b);
				} else if (s.getType().equals(EntityConstants.BINDING_JMS)) {
					Object b = DaoManager.getDao().get(SoaJmsBinding.class,
							s.getBindingId());
					DaoManager.getDao().delete(b);
				} else if (s.getType().equals(EntityConstants.BINDING_SOAP)) {
					Object b = DaoManager.getDao().get(SoaSoapBinding.class,
							s.getBindingId());
					DaoManager.getDao().delete(b);
				}
				DaoManager.getDao().delete(s);

			}

			DaoManager.getDao().flush();
			tx.commit();
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusBindingDao.class);
			logger.error("BusBindingDao.removeBindingByCatalogId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeBindingByCatalogId() error", e);
		} finally {
			tx.end();
		}

	}

	public static List getBusBindings(String busId) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String hql = "from SoaBusBinding s where s.busId = ?";
			List ret = DaoManager.getDao().find(hql, busId);
			List l = new ArrayList();
			for (int i = 0, j = ret.size(); i < j; i++) {
				SoaBusBinding s = (SoaBusBinding) ret.get(i);
				BusBindingWrapper cbw = new BusBindingWrapper();
				cbw.setBusBinding(s);
				if (s.getType().equals(EntityConstants.BINDING_HTTP)) {
					SoaHttpBinding b = (SoaHttpBinding) DaoManager.getDao()
							.get(SoaHttpBinding.class, s.getBindingId());

					cbw.setHttpBinding(b);

				} else if (s.getType().equals(EntityConstants.BINDING_JMS)) {
					SoaJmsBinding b = (SoaJmsBinding) DaoManager.getDao().get(
							SoaJmsBinding.class, s.getBindingId());
					SoaNormalJms info = (SoaNormalJms) DaoManager.getDao().get(
							SoaNormalJms.class, b.getInfoId());

					SoaJmsBindingWrapper jms = new SoaJmsBindingWrapper();
					jms.setInfo(info);
					jms.setJmsBinding(b);
					cbw.setJmsBinding(jms);
				} else if (s.getType().equals(EntityConstants.BINDING_SOAP)) {
					SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
							.get(SoaSoapBinding.class, s.getBindingId());
					cbw.setSoapBinding(b);
				} else if (s.getType().equals(
						EntityConstants.BINDING_WEB_SERVICES)) {
					SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
							.get(SoaSoapBinding.class, s.getBindingId());
					cbw.setSoapBinding(b);
				}
				l.add(cbw);

			}

			tx.commit();
			return l;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusBindingDao.class);
			logger.error("BusBindingDao.getBusBindings()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getBusBindings() error", e);
		} finally {
			tx.end();
		}

	}

	public static Object getBusBinding(String busCode, String bindingName,
			String bindingType) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String sql = "from SoaServiceBus s where s.code = ? ";
			List<SoaServiceBus> buses = (List<SoaServiceBus>) DaoManager
					.getDao().find(sql, busCode);

			if (buses.size() == 0) {
				tx.commit();
				return null;
			}

			String hql = "from SoaBusBinding s where s.busId = ?";
			List ret = DaoManager.getDao().find(hql, buses.get(0).getId());

			Object rb = null;
			for (int i = 0, j = ret.size(); i < j; i++) {
				SoaBusBinding s = (SoaBusBinding) ret.get(i);
				if (s.getName().equals(bindingName)) {
					if (s.getType().equals(EntityConstants.BINDING_HTTP)) {
						SoaHttpBinding b = (SoaHttpBinding) DaoManager.getDao()
								.get(SoaHttpBinding.class, s.getBindingId());
						rb = b;

					} else if (s.getType().equals(EntityConstants.BINDING_JMS)) {
						SoaJmsBinding b = (SoaJmsBinding) DaoManager.getDao()
								.get(SoaJmsBinding.class, s.getBindingId());
						SoaNormalJms info = (SoaNormalJms) DaoManager.getDao()
								.get(SoaNormalJms.class, b.getInfoId());

						SoaJmsBindingWrapper jms = new SoaJmsBindingWrapper();
						jms.setInfo(info);
						jms.setJmsBinding(b);
						rb = jms;
					} else if (s.getType().equals(EntityConstants.BINDING_SOAP)) {
						SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, s.getBindingId());
						rb = b;
					} else if (s.getType().equals(
							EntityConstants.BINDING_WEB_SERVICES)) {
						SoaSoapBinding b = (SoaSoapBinding) DaoManager.getDao()
								.get(SoaSoapBinding.class, s.getBindingId());
						rb = b;
					}
					break;
				}

			}

			tx.commit();
			return rb;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(BusBindingDao.class);
			logger.error("BusBindingDao.getBusBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getBusBinding() error", e);
		} finally {
			tx.end();
		}

	}
}
